//index.js
//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.urlimg,

    id: '',

    head_num: 0,
    header: [
      { head: '未使用' },
      { head: '已使用/已过期' },
    ],
    cash: '',

    scrollHeight: 0,
  },

  //页头点击函数
  head_click: function (e) {
    var that = this;
    that.setData({
      head_num: e.currentTarget.dataset.index,
      scrollHeight: 0,
    })

    if (that.data.id == 1) {//叠加金加载
      that.less_reqCouponList(that.data.head_num);
    } else if (that.data.id == 2) {//立减金加载
      that.overlay_reqCouponList(that.data.head_num);
    } else if (that.data.id == 3) {//优惠劵加载
      that.discount_reqCouponList(that.data.head_num);
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({ id: options.id, })


    app.app_load(load_fn);

    function load_fn() {
      wx.showLoading({
        title: '加载中...',
      });

      // that.judgment();
      if (that.data.id == 1) {//叠加金加载
        that.less_reqCouponList(0);
        wx.setNavigationBarTitle({ title: '叠加金' })
      } else if (that.data.id == 2) {//立减金加载
        that.overlay_reqCouponList(0);
        wx.setNavigationBarTitle({ title: '立减金' })
      } else if (that.data.id == 3) {//优惠劵加载
        that.discount_reqCouponList(0);
        wx.setNavigationBarTitle({ title: '优惠劵' })
      }
    }
  },

  //说明提示
  prompted: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var cash = that.data.cash;

    if (cash[index].open) {
      cash[index].open = false;
    } else {
      for (var i = 0; i < cash.length; i++) {
        cash[i].open = false;
      }
      cash[index].open = true;
    }
    that.setData({
      cash: cash,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
 * 请求叠加金列表
 */
  less_reqCouponList: function (status) {
    var that = this;

    wx.request({
      url: app.url + '?controller=api_repeatreduction&action=listReduction',
      data: {
        isUsed: status,
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      },
      success: function (res) {
        wx.hideLoading();
        that.setData({
          cash: (res.data.result ? res.data.result: []),
        });
      },
    })
  },


  /**
 * 请求优惠券列表
 */
  overlay_reqCouponList: function (status) {
    var that = this;
    wx.request({
      url: app.url + '?controller=api_coupon&action=listCoupon',
      data: {
        isUsed: status,
        type: 1
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      },
      success: function (res) {
        wx.hideLoading();
        that.setData({
          cash: (res.data.result ? res.data.result : []),
        });
      },
    })
  },



  /**
   * 请求优惠劵列表
   */
  discount_reqCouponList: function (status) {
    var that = this;
    wx.request({
      url: app.url + '?controller=api_coupon&action=listCoupon',
      data: {
        isUsed: status
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      },
      success: function (res) {
        wx.hideLoading();
        that.setData({
          cash: (res.data.result ? res.data.result : []),
        });
      },
    })
  },


}) 
